import { createRouter, createWebHashHistory } from "vue-router";

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    component: () => import("../views/Home.vue"),
    //二级路由
    children: [
      {
        path: "/home",
        redirect: "/home/index",
      },
      {
        path: "index",
        component: () => import("../components/home/Index.vue"),
        //三级路由
        children: [
          {
            path: "/home/index",
            redirect: "/home/index/recommend",
          },
          {
            path: "/home/index",
            redirect: "/home/index/recommend",
          },
          {
            path: "recommend",
            component: () => import("../components/index/Recommend.vue"),
          },
          {
            path: "teach",
            component: () => import("../components/index/Teach.vue"),
          },
          {
            path: "voice",
            component: () => import("../components/index/Voice.vue"),
          },
        ],
      },
      {
        path: "teach",
        component: () => import("../components/home/Teach.vue"),
      },
      {
        path: "find",
        component: () => import("../components/home/Find.vue"),
      },
      {
        path: "mine",
        component: () => import("../components/home/Mine.vue"),
      },
    ],
  },
  {
    path: "/login",
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/login/:id",
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/registry",
    component: () => import("../views/Registry.vue"),
  },
  {
    path: "/activity",
    component: () => import("../views/Activity.vue"),
  },
  {
    path: "/shop",
    component: () => import("../views/Shop.vue"),
    children: [
      {
        path: "/shop",
        redirect: "/shop/index",
      },
      {
        path: "/shop/index",
        component: () => import("../views/shop/Index.vue"),
      },
      {
        path: "/shop/classification",
        component: () => import("../views/shop/ClassIfication.vue"),
      },
      {
        path: "/shop/shopping",
        component: () => import("../views/shop/ShopPing.vue"),
      },
      {
        path: "/shop/my",
        component: () => import("../views/shop/My.vue"),
      },
    ],
  },
  {
    path: "/news",
    component: () => import("../views/News.vue"),
    meta: {
      requirelogin: true,
    },
  },
  {
    path: "/details",
    component: () => import("../views/Details.vue"),
  },
  {
    path: "/search",
    component: () => import("../views/Search.vue"),
  },
  {
    path: "/comment",
    component: () => import("../views/Comment.vue"),
  },
  {
    path: "/service",
    component: () => import("../views/Service.vue"),
  },
  {
    path: "/edit",
    component: () => import("../views/Edit.vue"),
    children: [
      {
        path: "/edit",
        redirect: "/edit/index",
      },
      {
        path: "index",
        component: () => import("../components/edit/index.vue"),
      },
      {
        path: "index/:id",
        component: () => import("../components/edit/index.vue"),
      },
      {
        path: "name",
        component: () => import("../components/edit/name.vue"),
      },
      {
        path: "name/:id",
        component: () => import("../components/edit/name.vue"),
      },
      {
        path: "like",
        component: () => import("../components/edit/like.vue"),
      },
      {
        path: "signed",
        component: () => import("../components/edit/signed.vue"),
      },
      {
        path: "work",
        component: () => import("../components/edit/work.vue"),
      },
    ],
  },
  {
    path: "/sign",
    component: () => import("../views/Sign.vue"),
    meta: {
      requirelogin: true,
    },
  },
  {
    path: "/store",
    component: () => import("../views/Store.vue"),
  },
  {
    path: "/detailspage",
    component: () => import("../views/Detailspage.vue"),
    children: [
      {
        path: "/detailspage",
        redirect: "/detailspage/audio",
      },
      {
        path: "audio/:id",
        component: () => import("../components/detailspage/Audio.vue"),
        props: true,
      },
    ],
  },
  {
    path: "/vip",
    component: () => import("../views/Vip.vue"),
  },
  {
    path: "/video",
    component: () => import("../views/video.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import("../views/Notfound.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
/* 全球前卫 */
router.beforeEach((to, from, next) => {
  /*  console.log(to);
   console.log(from);
   next(); */
  if (to.meta.requirelogin) {
    //如果值为true 说明要登录
    if (localStorage.getItem("token")) {
      //说明已经登陆
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});

export default router;
